import datetime
from django.utils import timezone
from django.db import models

datetime_format = "%Y/%m/%d %H:%M:%S %Z"


class Device(models.Model):
	name = models.CharField(max_length=50, null=True)
	device_number = models.CharField(max_length=20, unique=True, null=True)
	product_id = models.IntegerField(null=True)
	is_online = models.BooleanField(default=False, null=True)
	location = models.CharField(max_length=50, null=True)
	create_time = models.DateTimeField(null=True)
	last_active_time = models.DateTimeField(null=True)
	peri_update = models.BooleanField(default=False, null=False)
	dimensions = models.CharField(max_length=50, default="")

	def __str__(self):
		return self.name


class Datapack(models.Model):
	device = models.ForeignKey(Device, on_delete=models.CASCADE)
	time = models.DateTimeField(null=True)
	hash = models.BigIntegerField(null=True)

	def __str__(self):
		return "{} | {}".format(timezone.localtime(self.time).strftime(datetime_format), self.device)


class Data(models.Model):
	device = models.ForeignKey(Device, on_delete=models.CASCADE)
	name = models.CharField(max_length=50, null=True)
	unit = models.CharField(max_length=10, null=True)
	hash = models.BigIntegerField(null=True)
	value = models.FloatField(null=True)
	time = models.DateTimeField(null=True)
	min_value = models.FloatField(null=True)
	max_value = models.FloatField(null=True)
	datapack = models.ForeignKey(Datapack, on_delete=models.CASCADE)

	def __str__(self):
		return "{} | {} {} | {}".format(self.name, self.value, self.unit, timezone.localtime(self.time).strftime(datetime_format))

